/* eslint no-unused-vars:0 */
import React from 'react';
import 'antd/dist/antd.css';
import './index.less';
import { Input, Icon, Row, Col, Table, Button, Checkbox, Form } from 'antd';
import { Link } from 'react-router-dom';

const Search = Input.Search;

// const data = [];
// for (let i = 0; i < 100; i++) {
//     data.push({
//         key: i,
//         content: <a>这里是政治部风险模块{i}</a>,
//         rtime: `2019/04/01`,
//         source: `新京报`,
//         description: '摘要： 25日，中国人民教育出版社确认将在语文教材中删除《陈涉世家》，引发“担忧民众效仿造反”的政治性猜测，在国内外舆论中产生一定的负面影响。'
//     });
// }
const columns = [
    {},
    {
        title: '内容',
        dataIndex: 'title'
    },
    {
        title: '发布时间',
        dataIndex: 'date'
    },
    {
        title: '作者',
        dataIndex: 'author'
    },
    {
        title: '来源',
        dataIndex: 'infoSource'
    }
];

class SituationTable extends React.Component {
    state = {
        contentList: {
            pages: {
                id: '',
                title: '',
                infoSource: '',
                totalElements: 0
            }
        },
        data: [],
        pagination: { total: 0 },
        startDate: '',
        endDate: '',
        selectedRowKeys: [],
        keyWords: ''
    };

    //加载页面时自动获取全部风险
    componentDidMount() {
        this.$post('/trends/search/myCommon', { typeId: '' })
            .then((res) => {
                console.log('res:', res);
                if (res) {
                    var data = res.pages.content;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }

                    this.setState({
                        data: data,
                        pagination: {
                            total: res.pages.totalElements
                        }
                    });
                }
            })
            .catch((e) => {
                console.log('error');
            });
    }

    rowSelection = {
        // selectedRowKeys:this.state.selectRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
            this.setState({
                selectedRowKeys: selectedRowKeys
            });
        }
    };

    //点击普通搜索按钮
    onSearch = (value) => {
        this.$post('/trends/search/myCommon', {
            keyword: value,
            pageSize: 5,
            typeId: ''
        })
            .then((res) => {
                console.log('res:', res);
                if (res) {
                    var data = res.pages.content;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }

                    this.setState({
                        data: data,
                        pagination: {
                            total: res.pages.totalElements
                        },
                        keyWords: value
                    });
                }
            })
            .catch((e) => {
                console.log('error');
            });
    };

    //页码变化时调用的函数
    changePage = (page) => {
        console.log(this.state.selectedRowKeys);
        this.setState(
            {
                current: page
            },
            () => {
                this.$post('/trends/search/myCommon', {
                    typeId: '',
                    pageSize: 5,
                    pageNum: page - 1
                })
                    .then((res) => {
                        console.log('res:', res);
                        if (res) {
                            var data = res.pages.content;
                            for (var i = 0; i < data.length; i++) {
                                data[i]['key'] = data[i].id;
                            }

                            this.setState({
                                data: data,
                                pagination: {
                                    total: res.pages.totalElements
                                }
                            });
                        }
                    })
                    .catch((e) => {
                        console.log('error');
                    });
            }
        );
    };

    download = () => {
        this.$post('/workBriefing/createWord', {
            keyWords: this.state.keyWords,
            idList: this.state.selectedRowKeys
        })
            .then((res) => {
                let content = res;
                // 组装a标签
                let elink = document.createElement('a');

                // // 设置下载文件名
                elink.download = 'word_briefing.doc';
                elink.style.display = 'none';
                let blob = new Blob([content]);
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);
            })
            .catch((e) => {
                console.log(e);
            });
    };

    render() {
        return (
            <div>
                <Row>
                    <Col span="12">
                        <h3>
                            全部风险:&nbsp;(共{this.state.pagination.total}条)
                        </h3>
                        <Icon
                            type="download"
                            onClick={this.download.bind(this)}
                            style={{ fontSize: '25px', marginTop: '10px' }}
                        />
                    </Col>
                    <Col span="12" className="overview-col2">
                        <Search
                            placeholder="关键词"
                            onSearch={(value) => this.onSearch(value)}
                            style={{
                                width: 200,
                                float: 'right',
                                marginTop: '9px'
                            }}
                        />
                    </Col>
                </Row>
                <Row className="overview-row2">
                    <Table
                        columns={columns}
                        expandedRowRender={(record) => (
                            <p style={{ margin: 0 }}>{record.text}</p>
                        )}
                        dataSource={this.state.data}
                        rowSelection={this.rowSelection}
                        selectedRowKeys={this.state.selectedRowKeys}
                        pagination={{
                            total: this.state.pagination.total,
                            onChange: this.changePage,
                            pageSize: 5
                        }}
                    />
                </Row>
            </div>
        );
    }
}

export default Form.create()(SituationTable);
